﻿
$(function() {
    var postParam = "{}";
    $.ajax({
        url: "/organization/list",
        cache: false,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        timeout: 2000,
        data: postParam,
        statusCode: {
            200: function(json) {
                buildOrganizationTree(json);
            }
        }
    });
    buildRoleAuthTable(getDemoRoleTemplateJson());
});

//创建组织的树
function buildOrganizationTree(data) {
    var orgTree = $("#organization-tree");
    if (orgTree != null)
        orgTree.remove();

    var treeRoot = "<ul id='organization-tree' class='ztree'></ul>";
    $("#content-left").append(treeRoot);

    var setting = {
        view: {
            dblClickExpand: false,
            showLine: true
        },
        callback: {
            beforeClick: beforeClick,
            onClick: onClick
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    function beforeClick(treeId, treeNode, clickFlag) {
        showDebugInfo("<p>beforeClick: Name:" + treeNode.name + " ID:" + treeNode.id + " Category:" + treeNode.category + "</p>");
        if (treeNode.category == "2") {
            buildStaffList(treeNode);
        }
        return (treeNode.click != false);
    }

    function onClick(e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        zTree.expandNode(treeNode); //单击展开节点
    }

    $.fn.zTree.init($("#organization-tree"), setting, data);
}

//创建员工列表
function buildStaffList(organization) {

    var data = "{'Id':'" + organization.id + "'}";
    $.ajax({
        url: "/Staff/List",
        cache: false,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        timeout: 2000,
        data: data,
        statusCode: {
            200: function(json) {
                var tableOld = $("#_staff_table");
                if (tableOld != null)
                    tableOld.remove();

                var table = "<table id='_staff_table'><caption>" + organization.name + " - 员工列表</caption>" +
                    "<tbody><tr>" +
                    "<th class='staffid-td'>编号</th>" +
                    "<th class='staffname-td'>姓名</th>" +
                    "<th class='staffsex-td'>性别</th>" +
                    "<th class='staffphone-td'>电话</th>" +
                    "<th class='staffemail-td'>邮件</th>" +
                    "<th class='staffcounter-td'>窗口</th>" +
                    "<th class='stafflogintime-td'>上次登录</th>" +
                    "<th class='staffrole-td'>角色</th>" +
                    "<th class='staffcreatetime-td'>创建时间</th>" +
                    "<th class='staffcreater-td'>创建人</th>" +
                    "<th class='staffmodifiedtime-td'>修改时间</th>" +
                    "<th class='staffmender-td'>修改人</th>";
                for (var i = 0; i < json.length; i++) {
                    var item = json[i];
                    //{"Id":"001","Name":"黄阳","Email":null,"MobilePhone":null,"OrganizationId":null,"RoleId":null,
                    //"CreatedTime":"0001-01-01T00:00:00","ModifiedTime":"0001-01-01T00:00:00","CreaterId":null,"MenderId":null,
                    //"IsDeleted":false,"Password":null,"Sex":0,"LoginDateTime":"0001-01-01T00:00:00","LoginCounterId":0,"IsLogin":false}
                    table = table + "<tr><td class='staffid-td'>" + item.Id +
                        "</td><td class='staffname-td'>" + item.Name +
                        "</td><td class='staffsex-td'>" + item.Sex +
                        "</td><td class='staffphone-td'>" + item.MobilePhone +
                        "</td><td class='staffemail-td'>" + item.Email +
                        "</td><td class='staffcounter-td'>" + item.LoginCounterId +
                        "</td><td class='stafflogintime-td'>" + $.getShortDateFormat(item.LoginDateTime) +
                        "</td><td class='staffrole-td'>" + item.RoleId +
                        "</td><td class='staffcreatetime-td'>" + $.getShortDateFormat(item.CreatedTime) +
                        "</td><td class='staffcreater-td'>" + item.CreaterId +
                        "</td><td class='staffmodifiedtime-td'>" + $.getShortDateFormat(item.ModifiedTime) +
                        "</td><td class='staffmender-td'>" + item.MenderId +
                        "</td></tr>";
                }
                table = table + "</tbody></table>";
                $("#_staff_table_body").append(table);
            }
        },
    });
}

//根据角色权限树进行表格的创建
function buildRoleAuthTable(data) {

    var tableDiv = $("#role-auth-list-table");
    if (tableDiv != null)
        tableDiv.remove();

    //创建树状表格时的递归用到的Node的ID变量
    var nodeId;

    //根据权限子项进行表格的行创建

    function buildAuthTemplateTableRow(rowDataItem, nodeParentId, isSub, srcCode) {
        var rowResult;
        var trId = "node" + "-" + nodeId;

        //创建权限项的一些子操作项

        function buildDetail(subAuths) {
            var box = "";
            if (typeof(subAuths) != 'undefined' && subAuths.length > 0) {
                for (var j = 0; j < subAuths.length; j++) {
                    var si = subAuths[j];
                    var c = "<input type='checkbox' " + ((si.Auth) ? "checked='checked'" : "") + "/><span>" + si.Info + "</span>";
                    box = box + c;
                }
            }
            return box;
        }

        if (isSub) { //当是权限子项时
            var trClass = "class = 'child-of-node-" + nodeParentId + "'";
            var detail = buildDetail(rowDataItem.SubAuth);
            rowResult = srcCode + "<tr id='" + trId + "' " + trClass + "><td class='column-id'>" + rowDataItem.Id + "</td>" +
                "<td class='column-info'>" + rowDataItem.Info + "</td>" +
                "<td class='column-checkbox'><input type='checkbox' " + ((rowDataItem.Auth) ? "checked='checked'" : "") + "/></td>" +
                "<td class='column-datalevel'><input type='text' value='" + rowDataItem.DataLevel + "' /></td>" +
                "<td class='column-detail'>" + detail + "</td></tr>";
        }
        else { //当是权限分类时
            rowResult = srcCode + "<tr id='" + trId + "'><td class='column-id'>" + rowDataItem.Id + "</td>" +
                "<td class='column-info'>" + rowDataItem.Info + "</td>" +
                "<td class='column-checkbox'>" +
                "<td class='column-datalevel'></td>" +
                "<td class='column-detail'></td></tr>";
        }
        nodeId++;
        if (typeof(rowDataItem.Items) != 'undefined' && rowDataItem.Items.length > 0) {
            for (var w = 0; w < rowDataItem.Items.length; w++) {
                var r = buildAuthTemplateTableRow(rowDataItem.Items[w], nodeParentId, true, "");
                srcCode = srcCode + r;
            }
        }
        return rowResult + srcCode;
    }

    nodeId = 1;
    var trsResult = "";
    for (var i = 0; i < data.length; i++) {
        var rowData = data[i];
        trsResult = trsResult + buildAuthTemplateTableRow(rowData, nodeId, false, "");
    }
    var table = "<div id='role-auth-list-table'><table id ='auth-tmplate'><caption>权限模板</caption>" +
        "<tbody><tr>" +
        "<th class='column-id'>功能编号</th>" +
        "<th class='column-info'>功能说明</th>" +
        "<th class='column-checkbox'>基本权限</th>" +
        "<th class='column-datalevel'>数据权限</th>" +
        "<th class='column-detail'>高级操作功能</th>" +
        "</tr>" + trsResult + "</tbody></table></div>";

    $("#content-right").append(table);
    nodeId = 1;
    $("#auth-tmplate").treeTable();
}

//返回一个组织的基本树数据
function getDemoOrgJson() {
    var json = [
        { id: 1, pId: 0, name: "上海市商业银行", type: "root", open: true },
        { id: 11, pId: 1, name: "宝山", type: "category" },
        { id: "a", pId: 11, name: "上海车站南路分理处", type: "organization" },
        { id: 111, pId: 11, name: "上海市安龙路支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市安顺路支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市安亭支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市鞍山路支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市八埭头支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市八一路支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市白杨路支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市宝钢国贸支行", type: "organization" },
        { id: 111, pId: 11, name: "上海市长兴支行", type: "organization" },
        { id: 12, pId: 1, name: "崇明", type: "category" },
        { id: 111, pId: 12, name: "上海市长阳支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市常熟路支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城东支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城市工业园区支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市城中路支行", type: "organization" },
        { id: 111, pId: 12, name: "上海市程家桥支行", type: "organization" },
        { id: 13, pId: 1, name: "嘉定", type: "category" },
        { id: 111, pId: 13, name: "上海市大华支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大连路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大连支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大木桥路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大宁支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大同路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市大统支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市德州路支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市滴水湖支行", type: "organization" },
        { id: 111, pId: 13, name: "上海市地铁人民广场站支行", type: "organization" },
        { id: 14, pId: 1, name: "青浦", type: "category" },
        { id: 111, pId: 14, name: "上海市崇明支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市崇明支行新河分理处", type: "organization" },
        { id: 111, pId: 14, name: "上海市川北支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市川沙支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市春申支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市打浦桥支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市大场支行", type: "organization" },
        { id: 111, pId: 14, name: "上海市大渡河路支行", type: "organization" },
        { id: 15, pId: 1, name: "松江", type: "category" },
        { id: 111, pId: 15, name: "上海市第二营业部", type: "organization" },
        { id: 111, pId: 15, name: "上海市第一支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市滇池路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市电气支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市丁香花园支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市丁香路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市定海路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市定西路支行", type: "organization" },
        { id: 111, pId: 15, name: "上海市东安路支行", type: "organization" },
        { id: 16, pId: 1, name: "金山", type: "category" },
        { id: 116, pId: 16, name: "上海市宝钢支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行场中分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行共富分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市宝山支行临江分理处", type: "organization" },
        { id: 116, pId: 16, name: "上海市堡镇中路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市报春路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北蔡支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北京西路支行", type: "organization" },
        { id: 116, pId: 16, name: "上海市北渔路支行", type: "organization" },
        { id: 17, pId: 1, name: "奉贤", type: "category" },
        { id: 117, pId: 17, name: "上海市北站支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市碧江路支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市碧云国际支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市博山路支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市不夜城支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市财富广场支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹家渡支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹杨新村第二支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市曹杨新村支行", type: "organization" },
        { id: 117, pId: 17, name: "上海市漕宝路支行", type: "organization" },
        { id: 18, pId: 1, name: "闵行", type: "category" },
        { id: 118, pId: 18, name: "上海市漕河泾开发区支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市昌化路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市昌里东路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长白支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长岛路支行", type: "organization" },
        { id: 118, pId: 18, name: "上海市长江支行", type: "organization" },
        { id: 19, pId: 1, name: "浦东", type: "category" },
        { id: 119, pId: 19, name: "上海市长宁支行曹家渡分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行长宁路第二分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行空港分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长宁支行娄山关路第二分理处", type: "organization" },
        { id: 119, pId: 19, name: "上海市长桥支行", type: "organization" },
        { id: 119, pId: 19, name: "上海市长寿路支行", type: "organization" }
    ];
    return json;
}

//返回一个角色的权限模板的测试数据
function getDemoRoleTemplateJson() {
    var demoRoleTemplateJson = [
        {
            Id: "G001",
            Auth: true,
            DataLevel: 1,
            Info: "本日明细",
            Items: [
                {
                    Id: "881",
                    Auth: true,
                    DataLevel: -1,
                    Info: "本日正在等待",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                },
                {
                    Id: "882",
                    Auth: false,
                    DataLevel: 0,
                    Info: "本日正在办理",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                },
                {
                    Id: "883",
                    Auth: true,
                    DataLevel: 0,
                    Info: "本日已完成",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                }]
        },
        {
            Id: "G005",
            Auth: false,
            DataLevel: 2,
            Info: "工作量统计",
            Items: [
                {
                    Id: "551",
                    Auth: true,
                    DataLevel: -1,
                    Info: "基本数据",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                },
                {
                    Id: "552",
                    Auth: false,
                    DataLevel: 0,
                    Info: "办理时长",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                },
                {
                    Id: "553",
                    Auth: true,
                    DataLevel: 0,
                    Info: "等待时长",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Items: []
                }]
        },
        {
            Id: "G009",
            Auth: false,
            DataLevel: 2,
            Info: "设备监控",
            Items: [{
                    Id: "551",
                    Auth: true,
                    DataLevel: -1,
                    Info: "基本数据",
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新设备增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导设备出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打设备印", Items: [] }],
                    Items: []
                }
            ]
        },
        {
            Id: "G010",
            Auth: false,
            DataLevel: 2,
            Info: "满意度报表",
            Items: [
                {
                    Id: "441",
                    Auth: true,
                    DataLevel: -1,
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Info: "按业务统计"
                },
                {
                    Id: "442",
                    Auth: false,
                    DataLevel: 0,
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Info: "按员工统计"
                },
                {
                    Id: "443",
                    Auth: true,
                    DataLevel: 0,
                    SubAuth: [
                        { Id: "WQ1", Auth: true, DataLevel: -1, Info: "新增", Items: [] },
                        { Id: "WQ2", Auth: false, DataLevel: 0, Info: "导出", Items: [] },
                        { Id: "WQ3", Auth: true, DataLevel: 0, Info: "打印", Items: [] }],
                    Info: "投诉统计"
                }]
        },
        { Id: "G014", Auth: true, DataLevel: 2, Info: "日志查询", Items: [] },
        { Id: "G015", Auth: true, DataLevel: 2, Info: "机构管理", Items: [] },
        { Id: "G016", Auth: true, DataLevel: 2, Info: "员工管理", Items: [] }
    ];
    return demoRoleTemplateJson;
}

$("#_621").live("click", function() {
    var treeObj = $.fn.zTree.getZTreeObj("organization-tree");
    var nodes = treeObj.getCheckedNodes(true);
    showDebugInfo("<p>beforeClick: Name:" + nodes.name + " ID:" + nodes.id + " Category:" + nodes.category + "</p>");
});